package com.wn.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.dao.HottravelinfoDao;
import com.wn.entity.Hottravelinfo;
import com.wn.service.HottravelinfoService;
import com.wn.utils.PageParam;
import com.wn.utils.Rsp;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * (Hottravelinfo)表服务实现类
 *
 * @author makejava
 * @since 2020-10-26 09:30:45
 */
@Service("hottravelinfoService")
@Transactional(rollbackFor = Exception.class)
public class HottravelinfoServiceImpl implements HottravelinfoService {
    @Resource
    private HottravelinfoDao hottravelinfoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Hottravelinfo queryById(String id) {
        return this.hottravelinfoDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
//     * @param offset 查询起始位置
//     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public PageInfo<Hottravelinfo> queryAllByLimit(PageParam<Hottravelinfo> pageParam) {
//        return this.guideinfoDao.queryAllByLimit(offset, limit);

        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Hottravelinfo> userEntityList=hottravelinfoDao.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Hottravelinfo> GuideinfoPageInfo = new PageInfo<Hottravelinfo>(userEntityList);

            return GuideinfoPageInfo;

        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Hottravelinfo> userEntityList=hottravelinfoDao.searchSomeByPage(pageParam.getModel());
            PageInfo<Hottravelinfo> GuideinfoPageInfo = new PageInfo<Hottravelinfo>(userEntityList);

            return GuideinfoPageInfo;

        }


    }

    /**
     * 新增数据
     *
     * @param hottravelinfo 实例对象
     * @return 实例对象
     */
    @Override
    public Rsp insert(Hottravelinfo hottravelinfo) {


        hottravelinfo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        hottravelinfo.setCreatetime(new Date());

        return hottravelinfoDao.insert(hottravelinfo)==1?Rsp.success("添加成功"):Rsp.fail().msg("添加失败");
    }

    /**
     * 修改数据
     *
     * @param hottravelinfo 实例对象
     * @return 实例对象
     */
    @Override
    public Rsp update(Hottravelinfo hottravelinfo) {
//        this.guideinfoDao.update(guideinfo);
//        return this.queryById(guideinfo.getId());

        return hottravelinfoDao.update(hottravelinfo)==1?Rsp.success(null).msg("修改成功"):Rsp.fail().msg("修改失败");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.hottravelinfoDao.deleteById(id) > 0;
    }


    public Rsp deleteLogical(Hottravelinfo hottravelinfo) {

        hottravelinfo.setIsdeleted(1);

        return hottravelinfoDao.update(hottravelinfo)==1?Rsp.success(null).msg("逻辑删除成功"):Rsp.fail().msg("逻辑删除失败");
    }
}